﻿using System.Collections;
using System.Collections.Generic;
using UI;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using Yunchang;

namespace AutoChess
{
    public class ACChessProbability : OPPage, IPointerClickHandler, IDragHandler, IPointerDownHandler, IPointerUpHandler
    {
        public GameObject currentRoot, nextRoot;
        public List<Text> currentLevel;
        public List<Text> nextLevel;
        public GameObject closeMask;
        public GameObject probRoot;


        protected override void DoOpen()
        {
            base.DoOpen();
            EventManager.AddEventListener(CommonEvent.ON_AUTOCHESS_PROBABILITY, Close);
            SetProbabilityData(AutoChessManager.Ins.cacheData.CurPlayerData.ShipLevel);
        }

        protected override void DoClose(bool immediately)
        {
            base.DoClose(immediately);
            EventManager.RemoveEventListener(CommonEvent.ON_AUTOCHESS_PROBABILITY, Close);
        }

        private void SetProbabilityData(int level)
        {
            var currentProb = Table.Blo.AutoChessBlo.GetChessLevelTable(level);
            if (currentProb != null)
            {
                var probsText = currentProb.chessRareWeight.Split(';');
                if (probsText == null || probsText.Length != currentLevel.Count)
                {
                    ExLog.LogError("autoChessLevel.xlsx:Table Data Error");
                    return;
                }
                for (int i = 0; i < currentLevel.Count; i++)
                {
                    var probText = probsText[i].Split(',');
                    if (probText == null || probText.Length != 2)
                    {
                        ExLog.LogError("autoChessLevel.xlsx:Table Data Error");
                        return;
                    }
                    currentLevel[i].text = probText[1] + "%";
                }
                currentRoot.SetActiveEx(true);
            }
            else
            {
                currentRoot.SetActiveEx(false);
            }
            var nextProb = Table.Blo.AutoChessBlo.GetChessLevelTable(level + 1);
            if (nextProb != null)
            {
                var probsText = nextProb.chessRareWeight.Split(';');
                if (probsText == null || probsText.Length != nextLevel.Count)
                {
                    ExLog.LogError("autoChessLevel.xlsx:Table Data Error");
                    return;
                }
                for (int i = 0; i < nextLevel.Count; i++)
                {
                    var probText = probsText[i].Split(',');
                    if (probText == null || probText.Length != 2)
                    {
                        ExLog.LogError("autoChessLevel.xlsx:Table Data Error");
                        return;
                    }
                    nextLevel[i].text = probText[1] + "%";
                }
                nextRoot.SetActiveEx(true);
            }
            else
            {
                nextRoot.SetActiveEx(false);
            }
            gameObject.SetActiveEx(true);
        }

        public void OnPointerClick(PointerEventData eventData)
        {
            ExLog.Log("+++++OnPointerClick");
            if (eventData.pointerCurrentRaycast.gameObject == closeMask)
            {
                Close();
            }
            PassEvent(eventData, ExecuteEvents.pointerClickHandler);
        }

        public void PassEvent<T>(PointerEventData data, ExecuteEvents.EventFunction<T> function)
        where T : IEventSystemHandler
        {
            List<RaycastResult> results = new List<RaycastResult>();
            EventSystem.current.RaycastAll(data, results);
            GameObject current = data.pointerCurrentRaycast.gameObject;
            if (current == closeMask)
            {
                for (int i = 0; i < results.Count; i++)
                {
                    if (results[i].gameObject != current)
                    {
                        ExecuteEvents.Execute(results[i].gameObject, data, function);
                    }
                }
            }
        }

        public void OnPointerDown(PointerEventData eventData)
        {
            ExLog.Log("+++++OnPointerDown");
            if (eventData.pointerCurrentRaycast.gameObject == closeMask)
            {
                probRoot.SetActiveEx(false);
            }
            PassEvent(eventData, ExecuteEvents.pointerDownHandler);
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            ExLog.Log("+++++OnPointerUp");
            PassEvent(eventData, ExecuteEvents.pointerUpHandler);
        }

        public void OnDrag(PointerEventData eventData)
        {
            PassEvent(eventData, ExecuteEvents.dragHandler);
        }
    }
}

